import { useState } from 'react';
import { useRequest } from 'ahooks';
import {
  fetchBrand,
  fetchModelStage,
  fetchModuleName,
  fetchSeries,
  fetchType,
} from '@/services/intelligent/trackingInfo';

const useOptions = (form: any) => {
  const [query, setQuery] = useState({
    brandId: '',
    seriesId: '',
  });
  const brank = useRequest(fetchBrand);

  const series = useRequest(fetchSeries, {
    manual: true,
  });

  const type = useRequest(fetchType, {
    manual: true,
  });

  const modelStage = useRequest(fetchModelStage);

  const moduleName = useRequest(fetchModuleName);

  const handleChange = (key: 'brandId' | 'seriesId', value: string) => {
    const newQuery = { ...query, [key]: value };

    if (key === 'brandId') {
      series.run(value);
      form.resetFields(['vehicleStyle', 'vehicleSeries']);
    } else {
      type.run(newQuery);
      form.resetFields(['vehicleSeries']);
    }

    setQuery(newQuery);
  };

  return {
    brank: brank.data,
    series: series.data,
    modelStage: modelStage.data,
    moduleName: moduleName.data,
    type: type.data,
    onChange: handleChange,
  };
};

export default useOptions;
